import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/components/Layout/index.vue'),
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home/index.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'about',
        name: 'About',
        component: () => import('@/views/About/index.vue'),
        meta: { title: '关于我们' }
      },
      {
        path: 'products',
        name: 'Products',
        component: () => import('@/views/Products/index.vue'),
        meta: { title: '产品服务' }
      },
      {
        path: 'products/:id',
        name: 'ProductDetail',
        component: () => import('@/views/Products/detail.vue'),
        meta: { title: '产品详情' }
      },
      {
        path: 'news',
        name: 'News',
        component: () => import('@/views/News/index.vue'),
        meta: { title: '新闻资讯' }
      },
      {
        path: 'news/:id',
        name: 'NewsDetail',
        component: () => import('@/views/News/detail.vue'),
        meta: { title: '新闻详情' }
      },
      {
        path: 'contact',
        name: 'Contact',
        component: () => import('@/views/Contact/index.vue'),
        meta: { title: '联系我们' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior() {
    return { top: 0 }
  }
})

// 路由守卫 - 设置页面标题
router.beforeEach((to, from, next) => {
  const title = to.meta.title as string
  if (title) {
    document.title = `${title} - WinsTech`
  }
  next()
})

export default router

